package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicUserService;
import com.ruoyi.record.domain.RecordDept;
import com.ruoyi.record.service.IRecordDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 科室调整记录Controller
 * 
 * @author ruoyi
 * @date 2023-05-26
 */
@Controller
@RequestMapping("/record/dept")
public class RecordDeptController extends BaseController
{
    private String prefix = "record/dept";

    @Autowired
    private IRecordDeptService recordDeptService;

    @Autowired
    private IBasicUserService basicUserService;

    @RequiresPermissions("record:dept:view")
    @GetMapping()
    public String dept(ModelMap mmap)
    {
        if(ShiroUtils.getSysUser().isAdmin() || ShiroUtils.getSubject().hasRole("postmanager")){
            mmap.put("showFlag", true);
        }else{
            mmap.put("showFlag", false);
        }
        return prefix + "/dept";
    }

    /**
     * 查询科室调整记录列表
     */
    @RequiresPermissions("record:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordDept recordDept)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin() && !ShiroUtils.getSubject().hasRole("postmanager")){
            recordDept.setIdCard(ShiroUtils.getLoginName());
        }
        List<RecordDept> list = recordDeptService.selectRecordDeptList(recordDept);
        return getDataTable(list);
    }

    /**
     * 导出科室调整记录列表
     */
    @RequiresPermissions("record:dept:export")
    @Log(title = "科室调整记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordDept recordDept)
    {
        List<RecordDept> list = recordDeptService.selectRecordDeptList(recordDept);
        ExcelUtil<RecordDept> util = new ExcelUtil<RecordDept>(RecordDept.class);
        return util.exportExcel(list, "科室调整记录数据");
    }

    @Log(title = "员工科室调整记录", businessType = BusinessType.IMPORT)
    @RequiresPermissions("record:dept:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecordDept> util = new ExcelUtil<RecordDept>(RecordDept.class);
        List<RecordDept> deptList = util.importExcel(file.getInputStream());
        String message = recordDeptService.importRecordDept(deptList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("record:dept:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecordDept> util = new ExcelUtil<RecordDept>(RecordDept.class);
        return util.importTemplateExcel("员工科室调整记录");
    }

    /**
     * 新增科室调整记录
     */
    @GetMapping("/add/{userId}")
    public String add(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BasicUser user = basicUserService.selectBasicUserById(userId);
        mmap.put("user", user == null ? new BasicUser() : user);
        return prefix + "/add";
    }

    /**
     * 新增保存科室调整记录
     */
    @RequiresPermissions("record:dept:add")
    @Log(title = "科室调整记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordDept recordDept)
    {
        recordDept.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordDeptService.insertRecordDept(recordDept));
    }

    /**
     * 修改科室调整记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RecordDept recordDept = recordDeptService.selectRecordDeptById(id);
        mmap.put("recordDept", recordDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存科室调整记录
     */
    @RequiresPermissions("record:dept:edit")
    @Log(title = "科室调整记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordDept recordDept)
    {
        recordDept.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordDeptService.updateRecordDept(recordDept));
    }

    /**
     * 删除科室调整记录
     */
    @RequiresPermissions("record:dept:remove")
    @Log(title = "科室调整记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordDeptService.deleteRecordDeptByIds(ids));
    }
}
